package ide.ccf.ccdesigner.editors;

import java.util.ArrayList;

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IEditorActionBarContributor;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.actions.ActionFactory;

import ide.ccf.ccdesigner.translate.Message;
import ide.ccf.ccdesigner.views.embedded.FormWindowW;

/**
 * 窗体编辑器操作栏构建类
 *
 */
public class DesignerActionBarContributor implements IEditorActionBarContributor
{
    @SuppressWarnings("rawtypes")
	private ArrayList actions = null;
    @SuppressWarnings("rawtypes")
	private ArrayList tools = null;
    private IActionBars actionbars;
    private UiEditor editor;

    /**
     * 设置激活编辑器
     */
    public void setActiveEditor(IEditorPart targetEditor)
    {
        if (targetEditor instanceof UiEditor)
        {
            editor = ((UiEditor)targetEditor);

            if (editor.formWindow() != null)
            {
                editor.formWindow().setActiveFormWindow();
                
                if (actions == null)
                    setupActions();

                updateTools();
            }
        }
    }

    /**
     * 获取激活编辑器
     * @return
     */
    public UiEditor activeEditor()
    {
        return editor;
    }

    /**
     * 初始化
     */
    public void init(IActionBars bars, IWorkbenchPage page)
    {
        actionbars = bars;
    }

    /**
     * 释放
     */
    public void dispose()
    {
    }

    /**
     * 构建操作列表
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
	private void setupActions()
    {
        actions = new ArrayList();
        IToolBarManager toolbar = actionbars.getToolBarManager();
        IMenuManager menubar = actionbars.getMenuManager();
        FormWindowW formwindow = activeEditor().formWindow();

        // 前7个操作是硬编码的，因为是集成进去的。
        DesignerAction act = new DesignerAction(this, 0, false);
        actionbars.setGlobalActionHandler(ActionFactory.CUT.getId(), act);
        actionbars.setGlobalActionHandler(IWorkbenchActionConstants.CUT_EXT, act);
        actions.add(act);

        act = new DesignerAction(this, 1, false);
        actionbars.setGlobalActionHandler(ActionFactory.COPY.getId(), act);
        actions.add(act);

        act = new DesignerAction(this, 2, false);
        actionbars.setGlobalActionHandler(ActionFactory.PASTE.getId(), act);
        actions.add(act);

        act = new DesignerAction(this, 3, false);
        actionbars.setGlobalActionHandler(ActionFactory.DELETE.getId(), act);
        actions.add(act);

        act = new DesignerAction(this, 4, false);
        actionbars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), act);
        actions.add(act);

        act = new DesignerAction(this, 5, false);
        actionbars.setGlobalActionHandler(ActionFactory.UNDO.getId(), act);
        actionbars.setGlobalActionHandler(IWorkbenchActionConstants.UNDO_EXT, act);
        actions.add(act);

        act = new DesignerAction(this, 6, false);
        actionbars.setGlobalActionHandler(ActionFactory.REDO.getId(), act);
        actions.add(act);

        MenuManager designermenu = new MenuManager(Message.DesignerActionBarContributor_0);
        menubar.insertBefore(IWorkbenchActionConstants.M_WINDOW, (designermenu));

        // 其它操作是动态的
        for (int actId = 7; actId < formwindow.staticActionCount() + 1; actId++)
        {
            act = new DesignerAction(this, actId, false);
            actions.add(act);
            
            if (act.hasIcon())
                toolbar.add(act);
            
            designermenu.add(act);
        }
        
        designermenu.insert(formwindow.staticActionCount() - 7, new Separator());

        MenuManager previewmenu = new MenuManager(Message.DesignerActionBarContributor_1);
        designermenu.add(previewmenu);

        for (int actId = formwindow.staticActionCount() + 1; actId < formwindow.actionCount(); actId++) 
        {
            act = new DesignerAction(this, actId, false);
            actions.add(act);
            
            if (act.hasIcon())
                toolbar.add(act);
            
            previewmenu.add(act);
        }

        tools = new ArrayList();
        toolbar.add(new Separator());
        designermenu.add(new Separator());

        MenuManager editmenu = new MenuManager(Message.DesignerActionBarContributor_2);
        designermenu.add(editmenu);

        // 工具
        for (int toolId = 0; toolId < formwindow.toolCount(); toolId++)
        {
            act = new DesignerAction(this, toolId, true);
            tools.add(act);
            
            if (act.hasIcon())
                toolbar.add(act);
            
            editmenu.add(act);
        }
    }

    /**
     * 工具栏
     * @return
     */
    public IActionBars actionBars()
    {
        return actionbars;
    }

    /**
     * 更新操作
     * @param actId
     */
    public void updateAction(int actId)
    {
        if (actions != null)
        {
            DesignerAction act = (DesignerAction)actions.get(actId);
            FormWindowW formwindow = activeEditor().formWindow();

            String name = formwindow.actionName(actId);
            
            if (name != act.getText())
               act.setText(formwindow.getNameByActionID(actId, false));
            
            act.setEnabled(formwindow.isEnabled(actId));
        }
    }

    /**
     * 更新工具
     */
    public void updateTools()
    {
        FormWindowW formwindow = activeEditor().formWindow();

        for (int toolId = 0; toolId < tools.size(); toolId++)
        {
            if (formwindow.currentTool() == toolId)
                ((DesignerAction)tools.get(toolId)).setChecked(true);
            else
                ((DesignerAction)tools.get(toolId)).setChecked(false);
        }
    }
}
